﻿using Mapster;
using Microsoft.AspNetCore.Mvc;
using Qy.Core;
using Qy.Scenario;
using Qy.Web.Authorize;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Qy.Web.Areas.Api;

[Route("Api/[controller]/[action]")]
[Verify]
public class ContentCateController : Base
{
    private readonly ApplicationContext _applicationContext;
    private readonly ContentCateService _contentCateService;

    public ContentCateController(ApplicationContext applicationContext, ContentCateService contentCateService)
    {
        _applicationContext = applicationContext;
        _contentCateService = contentCateService;
    }

    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.ContentCate_Index)]
    public async Task<IActionResult> CateTreeAsync()
    {
        var allTree = await _contentCateService.GetCatTree(await _applicationContext.ILoginUserInfoMethods.LoginUserInfoAsync());
        return Success(new { data = allTree });
    }

    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.ContentCate_Index)]
    public async Task<IActionResult> IndexAsync()
    {
        var pagedList = await _contentCateService.GetContentCates();
        return Success(new { data = pagedList });

    }

    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.ContentCate_AddOrEditView)]
    public async Task<IActionResult> AddOrEditAsync(int id)
    {
        var data = await _contentCateService.GetIdAsync(id);
        var user = await _applicationContext.ILoginUserInfoMethods.LoginUserInfoAsync();
        IEnumerable<CategoryViewModel> cateList = await _contentCateService.GetContentCateListAsync(user);
        return Success(new { data, cateList });
    }


    [HttpPost, ActionName("Create")]
    [ModelStateFilter]
    [ActionPermissionFilter(Permission = PermissionKeys.ContentCate_AddOrEdit)]
    [Log(Name = "新增内容类别")]
    public async Task<IActionResult> CreatePostAsync([FromBody] EditContentCate cateModel)
    {
        ContentCate contentCate = cateModel.Adapt<ContentCate>();
        var res = await _contentCateService.InsertAndDelCacheAsync(contentCate);
        if (res != null)
            return Success("成功新增内容类别！");
        else
            return Fail("新增类别失败");
    }


    [HttpPut, ActionName("Edit")]
    [ModelStateFilter]
    [ActionPermissionFilter(Permission = PermissionKeys.ContentCate_AddOrEdit)]
    [Log(Name = "修改内容类别")]
    public async Task<IActionResult> EditPostAsync([FromBody] EditContentCate cateModel, int dyEdit)
    {
        ContentCate contentCate = await _contentCateService.GetIdAsync(cateModel.Id);
        if (dyEdit == 1)
        {
            contentCate.Sort = cateModel.Sort;
            await _contentCateService.UpdateAndDelCacheAsync(contentCate);
            return Success("内容类别排序修改成功！");
        }
        contentCate.Pid = cateModel.Pid;
        contentCate.Name = cateModel.Name;
        contentCate.Theme = cateModel.Theme;
        contentCate.Ctheme = cateModel.Ctheme;
        contentCate.Ico = cateModel.Ico;
        contentCate.Sort = cateModel.Sort;
        contentCate.Num = cateModel.Num;
        contentCate.Type = cateModel.Type;
        contentCate.Status = cateModel.Status;
        contentCate.BannerAd = cateModel.BannerAd;
        contentCate.BannerLit = cateModel.BannerLit;
        contentCate.BannerHeader = cateModel.BannerHeader;
        await _contentCateService.UpdateAndDelCacheAsync(contentCate);
        return Success($"修改类别:{contentCate.Name} Id:  {cateModel.Id} ");

    }

    [HttpDelete, ActionName("Delete")]
    [ActionPermissionFilter(Permission = PermissionKeys.ContentCate_Delete)]
    [Log(Name = "删除内容类别")]
    public async Task<IActionResult> DeleteAsync(int id)
    {
        if (await _contentCateService.Delete(id))
            return Success("删除类别,Id： " + id );
        else
            return Fail("删除失败！可能因为当前类别下有内容！");
    }

}
